package com.yy.permissions_general.module.system.schedule.handler;

import cn.hutool.core.convert.Convert;
import com.yy.permissions_general.module.system.schedule.entity.ScheduleJob;
import com.yy.permissions_general.module.system.schedule.mapper.ScheduleJobMapper;
import org.quartz.CronTrigger;
import org.quartz.Scheduler;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: TODO 定时任务启动处理类
 * @Author: YangYI
 * @Date: 2022/1/24
**/
public class ScheduleStarted {

    @Resource
    private Scheduler scheduler;

    @Resource
    private ScheduleJobMapper scheduleJobMapper;

    @PostConstruct
    public void init() {
        List<ScheduleJob> scheduleJobList = scheduleJobMapper.selectList(null);
        for (ScheduleJob scheduleJob : scheduleJobList) {
            CronTrigger cronTrigger = ScheduleHandler.getCronTrigger(scheduler, Convert.toLong(scheduleJob.getJobId()));
            if (cronTrigger == null) {
                ScheduleHandler.createJob(scheduler, scheduleJob);
            } else {
                ScheduleHandler.updateJob(scheduler, scheduleJob);
            }
        }
    }
}
